package com.drg.center.config;

import com.guaike.core.web.filter.AccessLogFilter;
import com.guaike.core.web.filter.CheckSignFilter;
import com.guaike.core.web.filter.CrossFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

import javax.annotation.Resource;

/**
 * @Author: zhangxilin
 * @Date: 2024/9/25
 */
@Configuration
public class FilterConfig {

    @Value("${spring.profiles.active}")
    private String env;

    @Resource
    private RedissonClient redissonClient;

    @Bean
    public FilterRegistrationBean crossFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        CrossFilter crossFilter = new CrossFilter();
        filterRegistrationBean.setFilter(crossFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("crossFilter");
        filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE +1);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean accessLogFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        AccessLogFilter accessLogFilter = new AccessLogFilter();
        filterRegistrationBean.setFilter(accessLogFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("accessLogFilter");
        filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE +2);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean checkSignFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        CheckSignFilter checkSignFilter = new CheckSignFilter();
        checkSignFilter.setEnv(env);
        filterRegistrationBean.setFilter(checkSignFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("checkSignFilter");
        filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE +3);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean authorityFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        AuthorityFilter authorityFilter = new AuthorityFilter();
        authorityFilter.setRedissonClient(redissonClient);
        filterRegistrationBean.setFilter(authorityFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("authorityFilter");
        filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 4);
        return filterRegistrationBean;
    }



}
